using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
using SolucionUtiles; 
using SolucionEntidad; 
namespace SolucionDatos 
{ 
   public class UbigeoDao 
    { 
        private SqlConnection cn; 
        public UbigeoDao(SqlConnection cn) 
        { 
           this.cn = cn; 
        } 
        public void insert(Ubigeo obj)
        { 
           SqlCommand command = new SqlCommand("INSERT INTO ubigeo " + 
           "(idUbigeo,departemento,provincia,distrito) VALUES" + 
           "(@idUbigeo,@departemento,@provincia,@distrito)", this.cn); 

command.Parameters.Add(new SqlParameter("@idUbigeo", obj.idUbigeo)); 
command.Parameters.Add(new SqlParameter("@departemento", obj.departemento)); 
command.Parameters.Add(new SqlParameter("@provincia", obj.provincia)); 
command.Parameters.Add(new SqlParameter("@distrito", obj.distrito)); 
command.ExecuteNonQuery(); 
} 
  		public void update(Ubigeo obj) 
    	{ 
        	SqlCommand command = new SqlCommand("UPDATE Ubigeo " + 
			" SET " + 
"departemento=@departemento" + 
",provincia=@provincia" + 
",distrito=@distrito" + 
 " WHERE " + 
"idUbigeo=@idUbigeo" 
,this.cn); 
command.Parameters.Add(new SqlParameter("@idUbigeo", obj.idUbigeo)); 
command.Parameters.Add(new SqlParameter("@departemento", obj.departemento)); 
command.Parameters.Add(new SqlParameter("@provincia", obj.provincia)); 
command.Parameters.Add(new SqlParameter("@distrito", obj.distrito)); 
command.ExecuteNonQuery(); 
} 
       public void delete(Ubigeo obj){ 
SqlCommand command = new SqlCommand("DELETE FROM ubigeo WHERE "  
 +"idUbigeo=@idUbigeo" 
, this.cn); 
command.Parameters.Add(new SqlParameter("@idUbigeo", obj.idUbigeo)); 
command.ExecuteNonQuery(); 
} 
        public Ubigeo get(Ubigeo obj) 
{ 
   Ubigeo record = null; 
    SqlCommand command = new SqlCommand("SELECT idUbigeo,departemento,provincia,distrito FROM ubigeo where " 
 +"idUbigeo=@idUbigeo" 
, this.cn); 
command.Parameters.Add(new SqlParameter("@idUbigeo", obj.idUbigeo)); 

    SqlDataReader dr = command.ExecuteReader();
dr.Read(); 
record = new Ubigeo(); 
if (dr["idUbigeo"] == DBNull.Value) { record.idUbigeo = SqlInt32.Null; } else { record.idUbigeo = (Int32)dr["idUbigeo"]; } 
if (dr["departemento"] == DBNull.Value) { record.departemento = SqlString.Null; } else { record.departemento = (String)dr["departemento"]; } 
if (dr["provincia"] == DBNull.Value) { record.provincia = SqlString.Null; } else { record.provincia = (String)dr["provincia"]; } 
if (dr["distrito"] == DBNull.Value) { record.distrito = SqlString.Null; } else { record.distrito = (String)dr["distrito"]; } 
this.cn.Close(); 
return record; 
} 
     public List<Ubigeo> getList() 
{ 
   List<Ubigeo> list = new List<Ubigeo>(); 
    SqlCommand command = new SqlCommand("SELECT idUbigeo,departemento,provincia,distrito FROM ubigeo", this.cn); 
    SqlDataReader dr = command.ExecuteReader(); 
    while (dr.Read()) 
    { 
Ubigeo obj = new Ubigeo(); 
if (dr["idUbigeo"] == DBNull.Value) { obj.idUbigeo = SqlInt32.Null; } else { obj.idUbigeo = (Int32)dr["idUbigeo"]; } 
if (dr["departemento"] == DBNull.Value) { obj.departemento = SqlString.Null; } else { obj.departemento = (String)dr["departemento"]; } 
if (dr["provincia"] == DBNull.Value) { obj.provincia = SqlString.Null; } else { obj.provincia = (String)dr["provincia"]; } 
if (dr["distrito"] == DBNull.Value) { obj.distrito = SqlString.Null; } else { obj.distrito = (String)dr["distrito"]; } 
list.Add(obj); 
} 
return list; 
} 
} 
} 
